<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class db_mysql{
	public static $_instance;
	public $dbLink;
	public $Transactions;
	public $arrSql;
	public $pconnect;
	
	public function __construct($params = array()) {
		if (!$params['hostname'] || !$params['username'] || !$params['database']) {
			show_error('Mysql Server HostName or UserName or Password or DatabaseName is error in the config file!',500,'Mysql Server connect fail!');
		}
	
		if ($params['port'] && $params['port'] != 3306) {
			$params['hostname'] .= ':' . $params['port'];
		}
		$this->pconnect = $params['pconnect'];
		$linkfunction = ( TRUE == $params['pconnect'] ) ? 'mysql_pconnect' : 'mysql_connect';
		$this->dbLink = $linkfunction($params['hostname'], $params['username'], $params['password']);
	
		if (!$this->dbLink) {
			show_error('Error Message:' . mysql_error() . '<br/>Error Code:' . mysql_errno(),500,'Mysql Server connect fail!');
		} else {
			if (mysql_select_db($params['database'], $this->dbLink)) {
				mysql_query("SET NAMES {$params['charset']}", $this->dbLink);
				if (version_compare($this->getServerInfo(), '5.0.2', '>=')) {
					mysql_query("SET SESSION SQL_MODE=''", $this->dbLink);
				}
			} else {
				show_error('Mysql Server can not connect database table. Error Code:' . mysql_errno() . ' Error Message:' . mysql_error(),500,'Mysql Server connect fail!');
			}
		}
		return true;
	}
	
	public function query($sql){
		$this->arrSql[] = $sql;
		if( $result = mysql_query($sql, $this->dbLink) ){
			return $result;
		}else{
			show_error('SQL:'.$sql.'<br />Error Message:' . mysql_error(),500,'Query Error');
		}
	}
	
	public function setlimit($sql, $limit){
		return $sql. " LIMIT {$limit}";
	}
	
	public function getServerInfo() {
		return ($this->dbLink)?mysql_get_server_info($this->dbLink):false;
	}
	
	public function affected_rows(){
		return mysql_affected_rows($this->dbLink);
	}
	
	public function getTable($tbl_name){
		return $this->getArray("DESCRIBE {$tbl_name}");
	}
	
	public function getRow($sql) {
		if( !$result = $this->query($sql) )return false;
		$rows = mysql_fetch_assoc($result);
		mysql_free_result($result);
		return $rows;
	}
	
	public function getArray($sql){
		if( !$result = $this->query($sql) )return array();
		if( !mysql_num_rows($result) )return array();
		$rows = array();
		while($rows[] = mysql_fetch_array($result,MYSQL_ASSOC)){}
		mysql_free_result($result);
		array_pop($rows);
		return $rows;
	}
	
	public function insertId() {
		return ($id = mysql_insert_id($this->dbLink)) >= 0 ? $id : mysql_result($this->query("SELECT last_insert_id()"));
	}
	
	public function startTrans() {
		if ($this->Transactions == false) {
			if ($this->query('BEGIN')) {
				$this->Transactions = true;
			}
		}
		return true;
	}
	
	public function commit() {
		if ($this->Transactions == true) {
			if ($this->query('COMMIT')) {
				$this->Transactions = false;
			}
		}
		return true;
	}
	
	public function rollback() {
		if ($this->Transactions == true) {
			if ($this->query('ROLLBACK')) {
				$this->Transactions = false;
			}
		}
		return true;
	}
	
	public function __escape($value = null) {
		if(is_null($value))return 'NULL';
		if(is_bool($value))return $value ? 1 : 0;
		if(is_int($value))return (int)$value;
		if(is_float($value))return (float)$value;
		if(@get_magic_quotes_gpc())$value = stripslashes($value);
		return '\''.mysql_real_escape_string($value, $this->dbLink).'\'';
	}
	
	public function __destruct() {
		if(TRUE != $this->params['pconnect']) @mysql_close($this->dbLink);
	}
	
	public static function getInstance($params) {
		if (!self::$_instance) {
			self::$_instance = new self($params);
		}
		return self::$_instance;
	}
}